package com.qs.cwams.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.common.framework.config.CommonContextHolder;
import com.qs.cwams.mapper.AtteConfigMapper;
import com.qs.cwams.pojo.domain.AtteConfigDO;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author suncheng
 */
public class AtteStandard {
    private final AtteConfigMapper atteConfigMapper;

    private final LocalTime amStartTime;

    private final LocalTime amEndTime;

    private final LocalTime pmStartTime;

    private final LocalTime pmEndTime;

    private final long full;

    private final long rest;

    public AtteStandard() {
        atteConfigMapper = CommonContextHolder.getBean(AtteConfigMapper.class);
        AtteConfigDO amTimeConfig = atteConfigMapper.selectOne(new LambdaQueryWrapper<AtteConfigDO>()
                .eq(AtteConfigDO::getConfigCode, "AmTime"));
        String amStartTimeStr = amTimeConfig.getConfigValue().split("-")[0];
        amStartTime = LocalTime.of(Integer.parseInt(amStartTimeStr.split(":")[0]), Integer.parseInt(amStartTimeStr.split(":")[1]));
        String amEndTimeStr = amTimeConfig.getConfigValue().split("-")[1];
        amEndTime = LocalTime.of(Integer.parseInt(amEndTimeStr.split(":")[0]), Integer.parseInt(amEndTimeStr.split(":")[1]));

        AtteConfigDO pmTimeConfig = atteConfigMapper.selectOne(new LambdaQueryWrapper<AtteConfigDO>()
                .eq(AtteConfigDO::getConfigCode, "PmTime"));
        String pmStartTimeStr = pmTimeConfig.getConfigValue().split("-")[0];
        pmStartTime = LocalTime.of(Integer.parseInt(pmStartTimeStr.split(":")[0]), Integer.parseInt(pmStartTimeStr.split(":")[1]));
        String pmEndTimeStr = pmTimeConfig.getConfigValue().split("-")[1];
        pmEndTime = LocalTime.of(Integer.parseInt(pmEndTimeStr.split(":")[0]), Integer.parseInt(pmEndTimeStr.split(":")[1]));

        full = Duration.between(amStartTime, pmEndTime).toMinutes();
        rest = Duration.between(amEndTime, pmStartTime).toMinutes();
    }

    public long standard() {
        return full - rest;
    }

    public long rest() {
        return rest;
    }

    public TimeSlot getRestSlot(LocalDate date) {
        return new TimeSlot(LocalDateTime.of(date, amEndTime), LocalDateTime.of(date, pmStartTime));
    }

    public LocalTime getAmStartTime() {
        return amStartTime;
    }

    public LocalTime getAmEndTime() {
        return amEndTime;
    }

    public LocalTime getPmStartTime() {
        return pmStartTime;
    }

    public LocalTime getPmEndTime() {
        return pmEndTime;
    }
}
